package cn.wellsun.util.utildm2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Objects;
public class Question {
	private String questionText;
	private ArrayList<String> options;
	private String answer;
	public String getQuestionText() {
		return questionText;
	}
	public void setQuestionText(String questionText) {
		this.questionText = questionText;
	}
	public ArrayList<String> getOptions() {
		return options;
	}
	public void setOptions(ArrayList<String> options) {
		this.options = options;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public static ArrayList<Question> readQuestions(String filename) {
		ArrayList<Question> questions = new ArrayList<>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			String line = "";
			int counter = 0;
			Question q = null;
			while ((line = reader.readLine()) != null) {
				if (counter % 5 == 0) {
					q = new Question();
					q.setQuestionText(line);
				}
				else {
					if (line.charAt(0) == '*') {
						q.getOptions().add(line.substring(1));
						q.setAnswer(line.substring(1));
					}
					else {
						q.getOptions().add(line);
					}
				}
				if (counter % 5 == 4) {
					questions.add(q);
				}
				counter++;
			}
			reader.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return questions;
	}
	public Question() {
		options = new ArrayList<String>();
	}
	public static void main(String[] args) {
		String s = (Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath() + "questions/AstrologyQuiz.txt");
		File file = new File(s.substring(1));
		System.out.println();
		ArrayList<Question> questions = Question.readQuestions(s.substring(1));
		for (int i=0;i<questions.size();i++) {
			System.out.println("问题："+questions.get(i).getQuestionText());
			System.out.println("选项："+questions.get(i).getOptions());
			System.out.println("答案："+questions.get(i).getAnswer());
			System.out.println("*********************************");
		}
	}
}
